


if (typeof(Wicket) == "undefined")
Wicket = { };
Wicket.DateTime = { }

Wicket.DateTime.parseDate = function(pattern, value) {
numbers = value.match(/(\d+)/g);
if (numbers == null) return Number.NaN;
var day, month, year;
arrayPos = 0;
for (i = 0; i < pattern.length; i++) {
c = pattern.charAt(i);
while ((pattern.charAt(i) == c) && (i < pattern.length)) {
i++;
}
if (c == 'y') {
year = numbers[arrayPos++];
} else if (c == 'M') {
month = numbers[arrayPos++];
} else if (c == 'd') {
day = numbers[arrayPos++];
}
if (arrayPos > 2) break;
}
	if (year < 100) {
if (year < 70) {
year = year * 1 + 2000;
} else {
year = year * 1 + 1900;
}
}
var date = new Date();
date.setFullYear(year, (month - 1), day);
return date;
}

Wicket.DateTime.padDateFragment = function(value) {
return (value < 10 ? "0" : "") + value;
}

Wicket.DateTime.getViewportHeight = function() {
if (window.innerHeight) 
viewPortHeight = window.innerHeight;
else if (document.documentElement && document.documentElement.clientHeight) 
viewPortHeight = document.documentElement.height;
else if (document.body) 
viewPortHeight = document.body.clientHeight;
return viewPortHeight;
}

Wicket.DateTime.positionRelativeTo = function(subject, target) {
targetPos = YAHOO.util.Dom.getXY(target);
targetHeight = YAHOO.util.Dom.get(target).offsetHeight;
subjectHeight = YAHOO.util.Dom.get(subject).offsetHeight;
subjectWidth = YAHOO.util.Dom.get(subject).offsetWidth;
viewPortHeight = Wicket.DateTime.getViewportHeight();
viewPortWidth = YAHOO.util.Dom.getViewportWidth();
	scrollPos = [YAHOO.util.Dom.getDocumentScrollLeft(), YAHOO.util.Dom.getDocumentScrollTop()];
	if (targetPos[0] + subjectWidth > scrollPos[0] + viewPortWidth) {

YAHOO.util.Dom.setX(subject, Math.max(targetPos[0], viewPortWidth) - subjectWidth);
} else {
YAHOO.util.Dom.setX(subject, targetPos[0]);
}
if (targetPos[1] + targetHeight + 1 + subjectHeight > scrollPos[1] + viewPortHeight) {

YAHOO.util.Dom.setY(subject, Math.max(targetPos[1], viewPortHeight) - subjectHeight);
} else {
YAHOO.util.Dom.setY(subject, targetPos[1] + targetHeight + 1);
}
}

Wicket.DateTime.substituteDate = function(datePattern, date) {
day = date[2];
month = date[1];
year = date[0];
	if(datePattern.match(/\bdd\b/)) day = Wicket.DateTime.padDateFragment(day);
if(datePattern.match(/\bMM\b/)) month = Wicket.DateTime.padDateFragment(month);
if(datePattern.match(/\byy\b/)) year = Wicket.DateTime.padDateFragment(year % 100);
	return datePattern.replace(/d+/, day).replace(/M+/, month).replace(/y+/, year);
}

Wicket.DateTime.showCalendar = function(widget, date, datePattern) {
if (date) {
date = Wicket.DateTime.parseDate(datePattern, date);
if (!isNaN(date)) {
widget.select(date);
firstDate = widget.getSelectedDates()[0];
widget.cfg.setProperty("pagedate", (firstDate.getMonth() + 1) + "/" + firstDate.getFullYear());
widget.render();
}
}
widget.show();
}
Wicket.DateTime.init = function(cfg) {
cfg.dpJs = cfg.widgetId + "DpJs";
cfg.dp = cfg.widgetId + "Dp";
cfg.icon = cfg.widgetId +"Icon";
YAHOO.namespace("wicket");
if (cfg.calendarInit.pages && cfg.calendarInit.pages > 1) {
YAHOO.wicket[cfg.dpJs] = new YAHOO.widget.CalendarGroup(cfg.dpJs,cfg.dp, cfg.calendarInit);
} else {
YAHOO.wicket[cfg.dpJs] = new YAHOO.widget.Calendar(cfg.dpJs,cfg.dp, cfg.calendarInit);
}
YAHOO.wicket[cfg.dpJs].isVisible = function() { return YAHOO.wicket[cfg.dpJs].oDomContainer.style.display == 'block'; }
function showCalendar() {
Wicket.DateTime.showCalendar(YAHOO.wicket[cfg.dpJs], YAHOO.util.Dom.get(cfg.componentId).value, cfg.datePattern);
if (cfg.alignWithIcon) Wicket.DateTime.positionRelativeTo(YAHOO.wicket[cfg.dpJs].oDomContainer, cfg.icon);
}
YAHOO.util.Event.addListener(cfg.icon, "click", showCalendar, YAHOO.wicket[cfg.dpJs], true);
function selectHandler(type, args, cal) {
YAHOO.util.Dom.get(cfg.componentId).value = Wicket.DateTime.substituteDate(cfg.datePattern, args[0][0]);
if (cal.isVisible()) {
if (cfg.hideOnSelect) cal.hide();
if (cfg.fireChangeEvent) {
var field = YAHOO.util.Dom.get(cfg.componentId);
if (field.onchange != null && typeof(field.onchange) != 'undefined') field.onchange();
}
}
}
YAHOO.wicket[cfg.dpJs].selectEvent.subscribe(selectHandler,YAHOO.wicket[cfg.dpJs]);
YAHOO.wicket[cfg.dpJs].render();
}
YAHOO.register("wicket-date", Wicket.DateTime, {version: "1.3.0", build: "rc1"});